BTParallel = BTComposite:New()

local this = BTParallel
this.name = "Parallel Task"

function this:New()
	local o = {}
	setmetatable(o, self)
	self.__index = self
	o.childTasks = {}
	o.executionStatus = {}
	return o
end

function this:OnUpdate()
	if (not self:HasChild()) then
		self.executionStatus = BTTaskStatus.Failure
		return BTTaskStatus.Failure
	end
	for i=1 , #self.childTasks do
		local childTask = self.childTasks[i]
		if (not self.executionStatus[i]) then --第一次执行
			self.executionStatus[i] = childTask:OnUpdate()
			if (self.executionStatus[i] == BTTaskStatus.Failure) then
				self.executionStatus = BTTaskStatus.Failure
				return BTTaskStatus.Failure
			end
		elseif (self.executionStatus[i] == BTTaskStatus.Running) then --第二次以及以后执行
			self.executionStatus[i] = childTask:OnUpdate()
			if (self.executionStatus[i] == BTTaskStatus.Failure) then
				self.executionStatus = BTTaskStatus.Failure
				return BTTaskStatus.Failure
			end
		end
	end
	local childrenComplete = true
	for i = 1, #self.executionStatus do
		if (self.executionStatus[i] == BTTaskStatus.Running) then
			childrenComplete = false
			break
		end
	end
	if (childrenComplete) then
		self.executionStatus = BTTaskStatus.Success
		return BTTaskStatus.Success
	else
		self.executionStatus = BTTaskStatus.Running
		return BTTaskStatus.Running
	end
end